package com.leyou.item.web;

import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规格
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据分类查询规格组
     * @param categoryId
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id") Long categoryId){
        List<SpecGroup> specGroupList = groupService.query().eq("category_id", categoryId).list();
        List<SpecGroupDTO> specGroupDTOList = SpecGroupDTO.convertEntityList(specGroupList);
        return ResponseEntity.ok(specGroupDTOList);
    }

    /**
     * 查询规格参数集合
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false) Long groupId,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
        List<SpecParamDTO> specParamDTOList=paramService.querySpecParams(categoryId,groupId,searching);
        return ResponseEntity.ok(specParamDTOList);
    }

    /**
     *  新增规格组
     * @param groupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO groupDTO){
        SpecGroup specGroup = groupDTO.toEntity(SpecGroup.class);
        groupService.save(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param groupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO groupDTO){
        SpecGroup specGroup = groupDTO.toEntity(SpecGroup.class);
        groupService.updateById(specGroup);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param paramDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO paramDTO){
        SpecParam specParam = paramDTO.toEntity(SpecParam.class);
        paramService.save(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecGroupDTO paramDTO){
        SpecParam specParam = paramDTO.toEntity(SpecParam.class);
        paramService.updateById(specParam);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupAndSpecParamByCategoryId(@RequestParam("id") Long categoryId){
        List<SpecGroupDTO> specGroupDTOList=groupService.querySpecGroupAndSpecParamByCategoryId(categoryId);
        return ResponseEntity.ok(specGroupDTOList);
    }
}
